<template>
  <div class="container">
    <div class="app-container">
      <el-tree :data="dptData" :props="defaultProps" :expand-on-click-node="false" :default-expand-all="true">
        <template #default="{ data }">
          <el-row style="width:100%;height:40px" type="flex" justify="space-between" align="middle">
            <el-col>{{ data.name }}</el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }}</span>
              <!-- 下拉菜单 -->
              <el-dropdown @command="handleCommand($event, data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <!-- dialog弹窗 -->
    <AddDept ref="addDept" :dialog-visible.sync="isShowDialog" :name-arr="nameArr" :code-arr="codeArr" :current-node-id="currentNodeId" @render="getDepartmentList" />
  </div>
</template>
<script>
import { deleteDepartmentApi, getDepartmentListApi } from '@/api/department'
import { transListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'

export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      dptData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      isShowDialog: false,
      nameArr: [], // 存储部门名称
      codeArr: [], // 存储部门编码,
      currentNodeId: '' // 当前点击的部门id
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      const res = await getDepartmentListApi()
      // 存储到nameArr和codeArr中
      this.nameArr = res.map(item => item.name)
      this.codeArr = res.map(item => item.code)
      // 调用递归函数将响应数据转化为树形结构
      // 调用递归函数将响应数据转化为树形结构
      this.dptData = transListToTreeData(res, 0)
    },
    async handleCommand(command, id) {
      if (command === 'add') {
        this.currentNodeId = id + '' // 将当前点击的部门id存储起来
        this.isShowDialog = true
      } else if (command === 'edit') {
        this.currentNodeId = id + '' // 将当前点击的部门id存储起来
        await this.$refs.addDept.getDptDetail(id) // 调用子组件中的方法获取当前点击的部门详情
        this.isShowDialog = true
      } else if (command === 'del') {
        this.$confirm('您确认要删除该部门吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
          // type: 'warning'
        }).then(async() => {
          await deleteDepartmentApi(id)
          this.getDepartmentList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(() => {

        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;

  .tree-manager {
    width: 80px;
    display: inline-block;
    margin: 10px;
  }
}
</style>
